import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:get/get.dart';

import '../controllers/server_settings_controller.dart';

class ServerSettingsView extends GetView<ServerSettingsController> {
  final _formKey = GlobalKey<FormState>();
  ServerSettingsView({super.key});
  @override
  Widget build(BuildContext context) {
    TextEditingController serverAddressController =
        TextEditingController(text: controller.serverAddress);

    TextEditingController serverPortController =
        TextEditingController(text: controller.serverPort);

    String? serverAddressValidator(String? address) {
      if (address!.isEmpty) {
        return 'input_server_address'.tr;
      }

      RegExp ipRegex = RegExp(
          r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
      if (!ipRegex.hasMatch(address)) {
        return 'address_format_wrong'.tr;
      }

      return null;
    }

    void doSave() async {
      if (!_formKey.currentState!.validate()) {
        return;
      }
      // 保存到设置
      controller.save(serverAddressController.text, serverPortController.text);
      Get.back();
    }

    var serverAddressField = TextFormField(
      controller: serverAddressController,
      validator: serverAddressValidator,
      style: const TextStyle(
        fontSize: 20,
        fontWeight: FontWeight.normal,
      ),
      decoration: InputDecoration(labelText: 'input_server_address'.tr),
      inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'[0-9,\.]'))],
    );

    var serverPortField = TextFormField(
      controller: serverPortController,
      validator: (value) => value!.isEmpty ? 'input_server_port'.tr : null,
      style: const TextStyle(
        fontSize: 20,
        fontWeight: FontWeight.normal,
      ),
      decoration: InputDecoration(labelText: 'input_server_port'.tr),
      inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'[0-9]'))],
    );

    var saveButton = ElevatedButton(
      onPressed: doSave,
      child: SizedBox(
        width: 350,
        height: 50,
        child: Center(
          child: Text(
            'save'.tr,
            style: const TextStyle(fontSize: 20, fontWeight: FontWeight.normal),
          ),
        ),
      ),
    );

    return Scaffold(
      appBar: AppBar(
        title: Text('server_settings'.tr),
        centerTitle: true,
      ),
      body: SingleChildScrollView(
        child: Center(
            child: Padding(
          padding: const EdgeInsets.all(20.0),
          child: Column(
            children: [
              const SizedBox(height: 120),
              Text("input_address_port".tr,
                  style: Theme.of(context).textTheme.headlineMedium),
              const SizedBox(height: 60),
              SizedBox(
                width: 360,
                child: Form(
                  key: _formKey,
                  child: Column(
                    children: [
                      serverAddressField,
                      const SizedBox(height: 50),
                      serverPortField,
                      const SizedBox(height: 30),
                    ],
                  ),
                ),
              ),
              const SizedBox(height: 40),
              saveButton,
            ],
          ),
        )),
      ),
    );
  }
}
